package com.shop.cereshop.business.dl.logic.controller;

import com.github.pagehelper.PageInfo;
import com.shop.cereshop.business.dao.dl.CareShopNewsColumnDAO;
import com.shop.cereshop.business.dl.logic.domian.CareShopNews;
import com.shop.cereshop.business.dl.logic.service.CareShopNewsService;
import com.shop.cereshop.business.dl.logic.utils.ResultInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author: 况鑫威
 * @Date: 2021/6/15
 * @Time: 13:53
 * @ClassName: DlCareShopNewsController
 * @Description: com.shop.cereshop.business.dl.logic.controller
 */
@RestController
@Slf4j(topic=("DlCareShopNewsController"))
@RequestMapping("/business")
@CrossOrigin
public class DlCareShopNewsController {
    @Autowired
    private CareShopNewsService careShopNewsService;
    @Autowired
    private CareShopNewsColumnDAO careShopNewsColumnDAO;

    /**
     * 功能描述: 商家端-添加/修改资讯
     * @Param: [careShopNews]
     * @Return: com.shop.cereshop.business.dl.logic.utils.ResultInfo
     * @Author: 况鑫威
     * @Date: 2021/6/15
     * @Time: 14:48
     */
    @PostMapping("/updateNews")
    public ResultInfo updateNews(CareShopNews careShopNews, HttpServletRequest request, Long columnId){
        //获取当前登录账户
//        CerePlatformBusiness user = (CerePlatformBusiness) request.getAttribute("user");
        if(careShopNews.getShopId() == null){
            return ResultInfo.error("缺少shopId");
        }
        if (careShopNews.getProductId()==null){
            careShopNews.setProductId(0L);
            careShopNewsService.updateBynewsId(careShopNews.getNewsId());
        }else if(careShopNews.getProductId() == 0){
            careShopNewsService.updateBynewsId(careShopNews.getNewsId());
        }
        if(careShopNews.getTitle() == null){
            return ResultInfo.error("缺少title");
        }
        if(careShopNews.getContent() == null){
            return ResultInfo.error("缺少内容");
        }
        if(careShopNews.getAuthor() == null){
            return ResultInfo.error("缺少author");
        }
        if(careShopNews.getMapPath() == null){
            return ResultInfo.error("缺少MapPath");
        }
        if(careShopNews.getColumnImageUrl() == null){
            return ResultInfo.error("缺少columnImageUrl");
        }
        if(careShopNews.getBannerFlag() == null){
            careShopNews.setBannerFlag("0");
        }

//        careShopNews.setBusinessUserId(user.getBusinessUserId());
        int i;
        //如果id不等于空，则说明是修改
        if(careShopNews.getNewsId() != null){
            i = careShopNewsService.updateByPrimaryKeySelective(careShopNews, columnId);
        //否则为增加
        }else{
            //初始化参数
            careShopNews.setHits(0);
            careShopNews.setRepeats(0);
            careShopNews.setAuditFlag("0");
            careShopNews.setPubTime(new Date());
            careShopNews.setDelFlag("0");
            i = careShopNewsService.insertSelective(careShopNews, columnId);
        }
        if(i <= 0){
            return ResultInfo.error("操作失败");
        }else {
            return ResultInfo.ok();
        }
    }


    /**
     * 功能描述: 商家端-删除资讯
     * @Param: [newsIds]
     * @Return: com.shop.cereshop.business.dl.logic.utils.ResultInfo
     * @Author: 况鑫威
     * @Date: 2021/6/15
     * @Time: 14:57
     */
    @PostMapping("/delNews")
    public ResultInfo delNews(Long[] newsIds){
        int i = 0;
        try{
           i = careShopNewsService.delNews(newsIds);
        }catch (Exception e){
            e.printStackTrace();
            log.info(e.toString());
        }
        if(i <= 0){
            return ResultInfo.error("操作失败");
        }else {
            return ResultInfo.ok();
        }
    }


    /**
     * 功能描述: 商家端-查询资讯列表
     * @Param: [columnId, page, row]
     * @Return: com.shop.cereshop.business.dl.logic.utils.ResultInfo
     * @Author: 况鑫威
     * @Date: 2021/6/15
     * @Time: 15:29
     */
    @PostMapping("/getNewsList")
    public ResultInfo getNewsList(Long columnId, Integer page, Integer row, String title){
        List newsList = careShopNewsService.getNewsList(columnId, page, row,title);
        PageInfo list = new PageInfo<>();
        list.setList(newsList);
        int total = careShopNewsColumnDAO.getAllcount(columnId);
        list.setTotal(total);
        return new ResultInfo(list);
    }
    //查看资讯详情
    @PostMapping("/informationDetails")
    public ResultInfo informationDetails(Long newsId){
        return new ResultInfo(careShopNewsService.selectByPrimaryKey(newsId));
    }


    /**
     * 功能描述: 资讯详情
     * @Param: [newsId]
     * @Return: com.shop.cereshop.business.dl.logic.utils.ResultInfo
     * @Author: 况鑫威
     * @Date: 2021/6/15
     * @Time: 16:25
     */
    @PostMapping("/getNewsParticulars")
    public ResultInfo getNewsParticulars(Long newsId){
        return new ResultInfo(careShopNewsService.selectByPrimaryKey(newsId));
    }
}
